<?php
namespace User\Controller;

use Common\Controller\BaseController;
class MainController extends BaseController
{
    public $userID;
    public $userName;
    public $userType;
    public $baseInfo = false;

    public function _initialize()
    {
        parent::_initialize();
        $this->userStatus = $this->checkUserStatus();
        if (!$this->userStatus) {
            $this->redirect(U("Home/Login/index"));
        }
        $this->userID = \Think\Crypt\Driver\Xxtea::decrypt(cookie('userID'), C('cookie_fix'));
        $this->userName = \Think\Crypt\Driver\Xxtea::decrypt(cookie('userName'), C('cookie_fix'));
        $this->userType = \Think\Crypt\Driver\Xxtea::decrypt(cookie('userType'), C('cookie_fix'));

        $this->assign('userID', $this->userID);
        $this->assign('userName', $this->userName);
        $this->assign('userType', $this->userType);
        $user = M('User')->where('userid=' . $this->userID)->find();
        if ($user) {
            $this->baseInfo = true;
            $user['email'] = $this->userName;
            $this->assign('user', $user);
        }
        M("User")->where("userid=" . $this->userID . "")->setField("last_action_time", time());
    }

    public function checkUserStatus()
    {
        $userID = cookie('userID');
        $userName = cookie('userName');
        $userType = \Think\Crypt\Driver\Xxtea::decrypt(cookie('userType'), C('cookie_fix'));
        if (empty($userName) || empty($userID) || $userType != 0) {
            return false;
        } else {
            return true;
        }
    }
}